import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:dio/dio.dart';

import '../../widget/mq_text.dart';
import '../../widget/mq_button.dart';
import '../../services/screen_adapter.dart';
import '../../config/config.dart';
import '../../services/storage.dart';
import '../tabs.dart';

class RegisterThird extends StatefulWidget {

  final Map arguments;

  RegisterThird({Key key, this.arguments}) : super(key: key);

  @override
  _RegisterThirdState createState() => _RegisterThirdState();
}

class _RegisterThirdState extends State<RegisterThird> {

  String tel;
  String code;
  String password = '';
  String rePassword = '';

  @override
  void initState() {
    super.initState();
    tel = widget.arguments['tel'];
    code = widget.arguments['code'];
  }

  /// 注册
  doRegister() async{

    /// 判断密码，确认密码
    if(password.length < 6) {
      Fluttertoast.showToast(
        msg: "密码长度不能小于六位",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.grey[600],
        textColor: Colors.white,
        fontSize: 16.0
      );
    } else if(password != rePassword) {
      Fluttertoast.showToast(
        msg: "密码和确认密码不一致",
        toastLength: Toast.LENGTH_SHORT,
        gravity: ToastGravity.CENTER,
        timeInSecForIosWeb: 1,
        backgroundColor: Colors.grey[600],
        textColor: Colors.white,
        fontSize: 16.0
      );
    } else {
      var api = '${Config.domain}api/register';
      var response = await Dio().post(api, data: {'tel': tel, 'code': code, 'password': password});
      if(response.data['success']) {
        /// 保存用户信息
        Storage.setString('userInfo', json.encode(response.data['userinfo']));
        /// 返回到根
        Navigator.of(context).pushAndRemoveUntil(new MaterialPageRoute(builder: (context) => new Tabs()),
         (route) => false);
      } else {
        Fluttertoast.showToast(
          msg: "${response.data['message']}",
          toastLength: Toast.LENGTH_SHORT,
          gravity: ToastGravity.CENTER,
          timeInSecForIosWeb: 1,
          backgroundColor: Colors.grey[600],
          textColor: Colors.white,
          fontSize: 16.0
        );
      }
    }

    
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init();
    return Scaffold(
      appBar: AppBar(
        title: Text('用户注册-第三步')
      ),
      body: Container(
        padding: EdgeInsets.all(ScreenAdapter.width(20)),
        child: ListView(
          children: <Widget>[
            SizedBox(height: 50),
            MqText(
              text: '请输入密码',
              password: true,
              onChange: (value) {
                setState(() => password = value);
              }
            ),
            SizedBox(height: 10),
            MqText(
              text: '确认密码',
              password: true,
              onChange: (value) {
                 setState(() => rePassword = value);
              }
            ),
            SizedBox(height: 20),
              MqButton(
                text: '注册',
                color: Colors.red,
                height: 74,
                callback: doRegister,
              )
          ]
        )
      )
    );
  }
}
